package com.biz.controller.goods;

import com.biz.model.Pmodel.base.PWxGoods;
import com.biz.model.Pmodel.base.PbasePics;
import com.biz.model.Pmodel.base.PbaseUser;
import com.biz.model.Pmodel.order.PorderDetail;
import com.biz.model.common.MessageBox;
import com.biz.service.MService.goods.GoodsServiceI;
import com.biz.service.MService.user.UserServiceI;
import com.biz.utils.ConfigUtil;
import com.biz.utils.StringUtil;
import com.framework.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author lzq
 */
@Controller
@RequestMapping(value="/goods")
public class GoodsController extends BaseController{
	@Resource(name="goodsService")
	private GoodsServiceI goodsService;

	@Resource(name = "userService")
	private UserServiceI userService;

	/**
	 * 跳转商品列表页
	 */
	@RequestMapping(value="toGoodsList")
	public ModelAndView toGoodsList()throws Exception{
		ModelAndView mv = new ModelAndView ();
		String openId = String.valueOf(this.getSession().getAttribute(ConfigUtil.get("SESSION_OPEN_ID")));
		PbaseUser pbaseUser = userService.queryUserInfoByOpenId(openId);
		List<PWxGoods> goodsList = goodsService.getGoodsList();
		mv.addObject("pbaseUser",pbaseUser);
		mv.addObject("goodsList",goodsList);
		mv.setViewName("goods/goodsList");
		return mv;
	}


	/**
	 * 跳转商品详情
	 */
	@RequestMapping(value="toGoodsDetail")
	public ModelAndView toGoodsDetail(String goodsId, String detailId)throws Exception{
		ModelAndView mv = new ModelAndView ();
		String openId = String.valueOf(this.getSession().getAttribute(ConfigUtil.get("SESSION_OPEN_ID")));
		PbaseUser pbaseUser = userService.queryUserInfoByOpenId(openId);
		PWxGoods pWxGoods = goodsService.getGoodsDetialById(goodsId);
		List<PbasePics> list = goodsService.getPicsByGoodsList(goodsId);
		mv.addObject("pageChargeType","newCharge");
		if(!StringUtil.isNullOrEmpty(detailId))
		{
			PorderDetail porderDetail = goodsService.getOrderDetailByCode(detailId);
			if(porderDetail !=null && porderDetail.getState() == 1)//待兑换
			{
				mv.addObject("pageChargeType","doCharge");
				mv.addObject("code",porderDetail.getCode());
			}else if(porderDetail !=null && porderDetail.getState() == 2){//完成
				mv.addObject("pageChargeType","alreadyCharge");
			}
		}
		mv.addObject("pbaseUser",pbaseUser);
		mv.addObject("pWxGoods",pWxGoods);
		mv.addObject("list",list);
		mv.addObject("goodsId",goodsId);
		mv.setViewName("goods/goodsDetial");
		return mv;
	}

	/**
	 * 兑换
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/doCharge")
	public void doCharge(HttpServletResponse response, String goodsId)throws Exception{
		MessageBox box = new MessageBox();
		String userId=String.valueOf(this.getRequest().getSession().getAttribute(ConfigUtil.get("SESSION_USER_ID")));
		box = goodsService.doCharge(goodsId,userId);
		writeJson(box,response);
	}


	/**
	 * 跳转兑换二维码页面
	 */
	@RequestMapping(value="toOrderQrcode")
	public ModelAndView toOrderQrcode(String code)throws Exception{
		ModelAndView mv = new ModelAndView ();
		String userId = String.valueOf(this.getSession().getAttribute(ConfigUtil.get("SESSION_USER_ID")));
		boolean flag = goodsService.testUserOrdersByUserIdAndCode(userId, code);
		if(flag)
		{
			mv.addObject("orderCode",code.trim());
			mv.setViewName("goods/goodsQrcode");
			return mv;
		}else{
			return new ModelAndView("redirect:/goods/toChargeHistory.ac");
		}
	}

	/**
	 * 跳转兑换记录
	 */
	@RequestMapping(value="toChargeHistory")
	public ModelAndView toChargeHistory()throws Exception{
		ModelAndView mv = new ModelAndView ();
		String userId = String.valueOf(this.getSession().getAttribute(ConfigUtil.get("SESSION_USER_ID")));
		List<Map<String,Object>> listHistory = goodsService.getChargeHistory(userId);

		mv.addObject("listHistory",listHistory);
		mv.setViewName("goods/chargeHistory");
		return mv;
	}

}
